package com.jingdianjichi.user.controller;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.common.Result;
import com.jingdianjichi.user.convert.UserConverter;
import com.jingdianjichi.user.entity.dto.UserDto;
import com.jingdianjichi.user.entity.req.UserPageReq;
import com.jingdianjichi.user.entity.req.UserReq;
import com.jingdianjichi.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/adduser")
    public Result addUser(@RequestBody UserReq userReq) {
        log.info("UserController.addUser.userReq:" + JSON.toJSONString(userReq));
        UserDto userDto = UserConverter.INSTANCE.convertReqToDto(userReq);
        int result = userService.addUser(userDto);
        return Result.ok(result);
    }

    @DeleteMapping("/deleteuser/{id}")
    public Result deleteUser(@PathVariable(value = "id") Integer id) {
        return Result.ok(userService.deleteUser(id));
    }

    @PostMapping("/list")
    public Result getPage(@RequestBody UserPageReq userPageReq) {
        log.info("userPageReq:" + JSON.toJSONString(userPageReq));
        UserDto userDto = UserConverter.INSTANCE.convertPageReqToDto(userPageReq);
        return Result.ok(userService.getUserPage(userDto));
    }

}
